home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / Sane / sane.py < prev   
Encoding:
Python Source  |  2000-06-23  |  2.2 KB  |  77 lines

  1.  
  2. import Image
  3. import _sane
  4.  
  5. from _sane import *
  6.  
  7. class Option:
  8.     def __init__(self, args):
  9.     import string
  10.     self.index, self.name = args[0], args[1]
  11.     self.title, self.desc = args[2], args[3]
  12.     self.type, self.unit  = args[4], args[5]
  13.     self.size, self.cap   = args[6], args[7]
  14.     self.constraint = args[8]
  15.     def f(x): 
  16.         if x=='-': return '_'
  17.         else: return x
  18.     if type(self.name)!=type(''): self.py_name=str(self.name)
  19.     else: self.py_name=string.join(map(f, self.name), '')
  20.  
  21. class SaneDev:
  22.     def __init__(self, devname): 
  23.     d=self.__dict__
  24.     d['dev']=_sane._open(devname)
  25.     d['opt']={} 
  26.  
  27.     optlist=d['dev'].get_options()
  28.     for t in optlist:
  29.         o=Option(t)
  30.         if o.type!=TYPE_GROUP:
  31.         d['opt'][o.py_name]=o
  32.  
  33.     def __setattr__(self, key, value):
  34.     dev=self.__dict__['dev']
  35.     optdict=self.__dict__['opt']
  36.     if not optdict.has_key(key):
  37.         self.__dict__[key]=value ; return
  38.     opt=optdict[key]
  39.     if opt.type==TYPE_GROUP:
  40.         raise AttributeError, "Groups can't be set: "+key
  41.     if not _sane.OPTION_IS_ACTIVE(opt.cap):
  42.         raise AttributeError, 'Inactive option: '+key
  43.     if not _sane.OPTION_IS_SETTABLE(opt.cap):
  44.         raise AttributeError, "Option can't be set by software: "+key    
  45.  
  46.     self.last_opt = dev.set_option(opt.index, value)
  47.     
  48.     def __getattr__(self, key):
  49.     dev=self.__dict__['dev']
  50.     optdict=self.__dict__['opt']
  51.     if not optdict.has_key(key):
  52.         raise AttributeError, 'No such attribute: '+key
  53.     opt=optdict[key]
  54.     if opt.type==TYPE_BUTTON:
  55.         raise AttributeError, "Buttons don't have values: "+key
  56.     if opt.type==TYPE_GROUP:
  57.         raise AttributeError, "Groups don't have values: "+key
  58.     if not _sane.OPTION_IS_ACTIVE(opt.cap):
  59.         raise AttributeError, 'Inactive option: '+key
  60.     self.last_opt, value = dev.get_option(opt.index)
  61.     return value
  62.  
  63.     def get_parameters(self): return self.dev.get_parameters()
  64.     def get_options(self): return self.dev.get_options()
  65.     def start(self): return self.dev.start()
  66.     def cancel(self): return self.dev.cancel()
  67.     def fileno(self): return self.dev.fileno()
  68.     def snap(self):
  69.     format, last_frame, (xsize, ysize), depth, bytes_per_line = self.get_parameters()
  70.     im=Image.new(format, (xsize,ysize))
  71.     self.dev.snap( im.im.id )
  72.     return im
  73.  
  74. def open(devname):
  75.     new=SaneDev(devname)
  76.     return new
  77.